<?php
namespace Lin\Dnah\Providers;
use DebugBar\DebugBarException;
use function foo\func;
use Illuminate\Support\ServiceProvider;
use Lin\Dnah\Manag;
use DebugBar\DataCollector\TimeDataCollector;

class  DnahServiceProvider extends ServiceProvider
{
    const SINGNAME="dnah";
    public function register()
    {
        $enabled = $this->app['config']->get('dnah.enabled');
        if(!$enabled){
            return ;
        }
        $this->app->booted(function (){
            $manag=app(self::SINGNAME);
            $manag->start();
        });
        $this->app->booting(function (){
            $this->app->singleton(self::SINGNAME,function ($app){
                $manag= new Manag($app);
                $bar=app('debugbar');
                $cor= null;
                if($bar!=null){
                    try{
                        $cor = $bar->getCollector('time');
                    }catch (DebugBarException $e){
                    }
                }
                $cor = $cor!=null ? $cor:new TimeDataCollector();

                $manag->setCollector($cor);
                return $manag ;
            });
        });

    }
    public function boot()
    {
        ///$app = $this->app;

        $configPath = __DIR__ . '/../../config/dnah.php';
        $this->publishes([$configPath => config_path('dnah.php')], 'config');

        // If enabled is null, set from the app.debug value
        $enabled = $this->app['config']->get('dnah.enabled');
        if (! $enabled) {
            return;
        }
        $manag=app(self::SINGNAME);

        register_shutdown_function(array(&$manag,'onShutdown'),config('dnah.shutdown_class'));

    }
}